package com.tgpms.web.element.controller;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.R;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.web.element.entity.WsElement;
import com.tgpms.web.element.service.IWsElementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物资设备属性从表 控制层
 * </p>
 *
 * @author jl
 * @since 2020-07-07
 */
@Api(value = "WsElementController", tags = "物资设备管理 D1127验收要素相关接口")
@RestController
@RequestMapping("/ev/WsElement")
public class WsElementController {

    private final Logger log = LoggerFactory.getLogger(WsElementController.class);

    @Resource
    private IWsElementService service;
    @Resource
    private TokenService tokenService;

    /**
     * Excel批量导入
     */
    @ApiOperation(value = "Excel批量导入物资设备属性接口", notes = "Excel批量导入物资设备属性接口", httpMethod = "POST")
    @PostMapping("/import")
    public Result batchImport(@RequestParam("file") MultipartFile file, @RequestParam("type") String type,
                              @RequestParam(value = "dataOwner", required = false) String dataOwner, HttpServletRequest request) {
        Result result = new Result();
        String insertName = tokenService.getUserName(request);
        try {
            Map<String, String> map = service.batchImport(file, type, dataOwner, insertName);
            if (map.get("code").equals("1")) {
                result.setSuccess(true);
                result.setData(map.get("data"));
            } else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }

    @ApiOperation(value = "新增物资设备属性从表接口", notes = "新增物资设备属性从表接口", httpMethod = "POST")
    @PostMapping(value = "/addWsElementInfo")
    public Result addWsElementInfo(@RequestBody WsElement en, HttpServletRequest request) {
        log.info("新增物资设备属性从表接口接收到的参数为: " + en.toString());
        Result result = new Result();
        try {
            String insertName = tokenService.getUserName(request);
            en.setInsertUser(insertName);
            result = service.addWsElement(en, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("增加异常");
            return result;
        }
        return result;
    }

    @ApiOperation(value = "修改物资设备属性从表接口", notes = "修改物资设备属性从表接口", httpMethod = "PUT")
    @PutMapping(value = "/updateWsElementInfo")
    public Result updateWsElementInfo(@RequestBody WsElement en, HttpServletRequest request) {
        log.info("修改物资设备属性从表接口接收到的参数为: " + en.toString());
        Result result = new Result();
        try {
            String insertName = tokenService.getUserName(request);
            en.setModifyUser(insertName);
            result = service.updateWsElement(en, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("修改异常");
            return result;
        }
        return result;
    }

    @ApiOperation(value = "删除物资设备属性从表接口", notes = "删除物资设备属性从表接口", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteWsElementInfo")
    public Result deleteWsElementInfo(@RequestParam(required = false) String commodityNumber,
    		@RequestParam(required = false) String wseCode) {
        log.info("删除物资设备属性从表接口接收到的参数为: commodityNumber:{},wseCode:{}",commodityNumber,wseCode);
        Result result = new Result();
        try {
            result = service.deleteWsElement(commodityNumber,wseCode, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("删除异常");
            return result;
        }
        return result;
    }
}
